package com.example.shiwu.service;

import android.content.Context;

import com.example.shiwu.db.dao.BoxThreeDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.BoxOne;
import com.example.shiwu.db.model.BoxThree;
import com.example.shiwu.manager.DatabaseHelper;
import com.example.shiwu.util.DateUtils;

import java.util.ArrayList;
import java.util.List;

public class BoxThreeService {

    private DatabaseHelper dbHelper;
    private BoxThreeDao<BoxThree> BoxThreeDao;
    private ArrayList<BoxThree> listBoxThree = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public BoxThreeService() {
    }

    public BoxThreeService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }


    /**获取单个数据
     * @param BoxThree
     * @return
     */
    public List<BoxThree> getBoxThree(BoxThree BoxThree) {
        BoxThreeDao = BaseDaoFactory.getInstance().createBaseDao(BoxThreeDao.class, BoxThree.class);
        listBoxThree = new ArrayList<>();
        listBoxThree.addAll(BoxThreeDao.query(BoxThree));
        return listBoxThree;
    }

    /**分页获取数据
     * @param BoxThree
     * @return
     */
    public List<BoxThree> getBoxThreeList(BoxThree BoxThree,Integer startIndex,Integer limit) {
        BoxThreeDao = BaseDaoFactory.getInstance().createBaseDao(BoxThreeDao.class, BoxThree.class);
        listBoxThree = new ArrayList<>();
        listBoxThree.addAll(BoxThreeDao.query(BoxThree,null,"create_time desc",null,startIndex,limit));
        return listBoxThree;
    }


    public boolean save(BoxThree BoxThree) {
        BoxThreeDao = BaseDaoFactory.getInstance().createBaseDao(BoxThreeDao.class, BoxThree.class);
        long size = BoxThreeDao.insert(BoxThree);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(BoxThree BoxThree) {
        BoxThreeDao = BaseDaoFactory.getInstance().createBaseDao(BoxThreeDao.class, BoxThree.class);
        BoxThree boxThree= new BoxThree();
        boxThree.setItemId(BoxThree.getItemId());
        List<BoxThree> lis =BoxThreeDao.query(boxThree);
        Boolean bl = true;
        if(lis.size()>0){
            BoxThree.setUpdateTime(DateUtils.parseDate());
            long size = BoxThreeDao.update(BoxThree,new BoxThree());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            BoxThree.setCreateTime(DateUtils.parseDate());
            long size = BoxThreeDao.insert(BoxThree);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updade(BoxThree BoxThree,BoxThree where) {
        long size = BoxThreeDao.update(BoxThree,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public Boolean delete(BoxThree boxThree) {
        int size = BoxThreeDao.delete(boxThree);
        if(size >0 ){
            return true;
        }
        return false;
    }
}
